package com.fjrcloud.service;

import com.fjrcloud.dao.IdentifiedHousingDao;
import com.fjrcloud.entity.*;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 2017/1/22.
 */
@Service
public class IdentifiedHousingService {

    @Autowired
    private IdentifiedHousingDao dao;
    @Autowired
    private HouseholdService householdService;
    @Autowired
    private LandStandardService landStandardService;
    @Autowired
    private HousingStructureService structureService;
    @Autowired
    private HousingDecorateService decorateService;


    public IdentifiedHousing save(
            Integer id,
            Integer masterId,
            Integer landStandardId,
            Integer structureLevelId,
            Integer structureIntoNewId,
            Integer decorateLevelId,
            Integer decorateIntoNewId,
            Integer layer,
            HousingNature nature,
            Basement basement,
            Boolean attic,
            Boolean atticHighly,
            Double area,
            String description) throws BusinessException {

        if (masterId == null)
            throw new BusinessException("认定主体无效");
        HouseholdMaster master = householdService.getMaster(masterId);
        if (master == null)
            throw new BusinessException("认定主体无效");

        if (landStandardId == null)
            throw new BusinessException("手续认定错误");
        LandStandard landStandard = landStandardService.get(landStandardId);
        if (landStandard == null)
            throw new BusinessException("手续认定错误");

        if (structureLevelId == null)
            throw new BusinessException("房屋结构错误");//错误
        HousingStructureLevel structureLevel = structureService.getStructureLevel(structureLevelId);
        if (structureLevel == null)
            throw new BusinessException("房屋结构错误");

        if (structureIntoNewId == null)
            throw new BusinessException("房屋结构成新率错误");
        HousingStructureIntoNew structureIntoNew = structureService.getStructureIntoNew(structureIntoNewId);
        if (structureIntoNew == null)
            throw new BusinessException("房屋结构成新率错误");

        if (decorateLevelId == null)
            throw new BusinessException("房屋装修档次错误");//错误
        HousingDecorateLevel decorateLevel = decorateService.getDecorateLevel(decorateLevelId);
        if (decorateLevel == null)
            throw new BusinessException("房屋装修档次错误");

        if (decorateIntoNewId == null)
            throw new BusinessException("房屋装修档次成新率错误");//错误
        HousingDecorateIntoNew decorateIntoNew = decorateService.getDecorateIntoNew(decorateIntoNewId);
        if (decorateIntoNew == null)
            throw new BusinessException("房屋装修档次成新率错误");

        if (layer < 0 && basement == null)
            throw new BusinessException("未选择地下层类型");

        if (area == null)
            throw new BusinessException("面积错误");//面积错误

        IdentifiedHousing identified;
        if (id == null) {
            IdentifiedHousing layerHousing = dao.findByValidAndMasterAndLayer(true,master,layer);
            if (layerHousing != null)
                throw new BusinessException("层数："+layerHousing.getLayer()+" 已经添加过了");
            identified = new IdentifiedHousing();
        }else{
            identified = dao.findOne(id);
        }

        if (identified == null)
            throw new BusinessException("无效的保存对象");//无效的保存对象

        identified.setMaster(master);
        identified.setLandStandard(landStandard);

        identified.setStructureLevel(structureLevel);
        identified.setStructureIntoNew(structureIntoNew);
        identified.setDecorateLevel(decorateLevel);
        identified.setDecorateIntoNew(decorateIntoNew);

        identified.setLayer(layer);
        identified.setBasement(basement);
        identified.setNature(nature);
        if(atticHighly != null && atticHighly)
            attic = true;
        identified.setAttic(attic == null ? false:attic);
        identified.setAtticHighly(atticHighly == null?false:atticHighly);
        identified.setArea(area);
        identified.setDescription(description);
        identified.setCreateTime(System.currentTimeMillis());

        return dao.save(identified);
    }
    public Page<IdentifiedHousing> find(Integer masterId, Integer pageNum, Integer pageSize) throws BusinessException {
        if (masterId == null)
            throw new BusinessException("无效认证主体信息");//无效认证主体信息
        HouseholdMaster master = householdService.getMaster(masterId);
        if (master == null)
            throw new BusinessException("无效认证主体信息");//无效认证主体信息

        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return dao.findByValidAndMasterOrderByLayerAsc(true, master, pageRequest);
    }
    public IdentifiedHousing get(Integer id){
        return dao.findOne(id);
    }
    public void del(Integer id){
        dao.delete(id);
    }
}
